package com.example.ffepeg.ffeImg;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//为图片添加水印
public class FfepngTest {
    public static boolean replaceWatermark(String inputImagePath, String outputImagePath, String watermarkImagePath, int x, int y) throws IOException {
        String command = String.format("ffmpeg -i %s -i %s -filter_complex \"overlay=%s:%s\" %s",
                inputImagePath, watermarkImagePath, x, y, outputImagePath);
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);

        try {
            process.waitFor();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IOException("水印替换过程中断: ", e);
        }
        return true;
    }

    public static boolean replaceWatermark(String inputImagePath, String outputImagePath, String watermarkImagePath, int width, int height, int x, int y) throws IOException {
        // 使用scale过滤器调整水印大小，并使用overlay过滤器将水印放在主图像上
        String command = String.format("ffmpeg -i %s -i %s -filter_complex \"[1:v]scale=%d:%d[s];[0:v][s]overlay=%d:%d\" %s",
                inputImagePath, watermarkImagePath, width, height, x, y, outputImagePath);

        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command);

        try {
            process.waitFor();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new IOException("水印替换过程中断: ", e);
        }
        return true;
    }

    public static Map<String, Object> kullFile(String imagePath) {  //必须本地路径
        Map<String, Object> map = new HashMap<>();
        try {
            // 读取图像文件
            File imageFile = new File(imagePath);
            BufferedImage bufferedImage = ImageIO.read(imageFile);

            if (bufferedImage == null) {
                System.out.println("无法读取图像文件，请检查路径是否正确以及文件是否损坏。");
            }

            // 获取图像的宽度和高度
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            map.put("width",width);
            map.put("height",height);
            // 打印图像的分辨率
            System.out.printf("图像的分辨率为: %d x %d%n", width, height);
            return map;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     *  根据图片的大小自定义水印的大小
     */
    public static void main(String[] args) {
        try {
            String inputImagePath = "C:\\Users\\Administrator\\Desktop\\260.png";

            String outputImagePath = "src/main/resources/out/file-ok12120-kll.png"; // 输出图片路径  768 *  1024
            String watermarkImagePath = "https://pushimg.liecaizhijia.com/image_example/img/23490682-7c98-43f1-a8ec-93c7b9bac77cimg.png"; // 水印图片路径
            String logo = "https://pushimg.liecaizhijia.com/image_example/img/c6dd68e8-31da-4af8-b255-14082a279fff51826714613360d6ca65957966e675a.png";

            Map<String, Object> map = kullFile(inputImagePath);
            Integer width = (Integer) map.get("width");  //宽 图片
            Integer height = (Integer) map.get("height");  //高 图片

            double heightRatio = 0.05;
            int newWatermarkHeight = (int)(height * heightRatio);
            int TargetWidth = newWatermarkHeight * 3;//目标

            int intput_X = width - TargetWidth - 10;
            int intput_Y = height - newWatermarkHeight - 10;

            boolean stream = replaceWatermark(inputImagePath, outputImagePath, logo ,TargetWidth,newWatermarkHeight,intput_X, intput_Y);
            System.out.println(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
